package com.mrd.serverconnect.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mrd.serverconnect.pkg.UpPKGHelp;

import java.util.ArrayList;
import java.util.List;


public class SPUtils {
    public static final  String LAST_LANGUAGE_IDENTIFIER = "last_language_identifier";//上次选择的语言包标识； // 若没有；则默认为简体中文；zh_rCN


    public static final String PASSENGER = "passenger";  //全部访客数据
    public static final String INNUM_NOW = "innum_now";  //当前在馆人数
    public static final String LARGENUM = "large_num";   //最大容纳量

    public static final String TITLE = "title"; //标题
    public static final String SUBTITLE1 = "subtitle1"; //标题
    public static final String SUBTITLE2 = "subtitle2"; //标题
    public static final String SUBTITLE3 = "subtitle3"; //标题
    public static final String SUBTITLE4 = "subtitle4"; //标题
    public static final String TOKEN = "token";
    public static final String BASEURL = "baseurl";

    public static final String SHOWDATA_POSITION = "showdata_position";
    public static final String SN = "sn";
    public static final String INITMONTH = "initmonth";
    public static final String INITYEAR = "inityear";
    public static final String INITHISTORY = "inithistory";

    public static final String CLEARANCE_TIME = "clearance_time";

    private static boolean defavalues = false;

    private static SharedPreferences sp;

    public static SharedPreferences getSp(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("mrj_config", 0);
        }

        return sp;
    }

    /**
     * @param context
     * @param key
     * @param value   boolean 标记
     */
    public static void putBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = getSp(context);

        sp.edit().putBoolean(key, value).apply();

    }

    /**
     * @param context
     * @param key
     * @param defavalue 默认值 false
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defavalue) {
        SharedPreferences sp = getSp(context);

        return sp.getBoolean(key, defavalue);

    }

    public static boolean getBoolean(Context context, String key) {

        return getBoolean(context, key, defavalues);

    }

    /**
     * @param context
     * @param key
     * @return
     * @Description 保存字符串
     * @author Jrking
     */

    public static void putString(Context context, String key, String value) {
        SharedPreferences sp = getSp(context);

        sp.edit().putString(key, value).apply();

    }

    public static String getString(Context context, String key, String defavalue) {
        SharedPreferences sp = getSp(context);

        return sp.getString(key, defavalue);

    }

    public static String getString(Context context, String key) {
        SharedPreferences sp = getSp(context);

        return sp.getString(key, "");

    }

    public static void putInt(Context context, String key, int value) {
        SharedPreferences sp = getSp(context);
        sp.edit().putInt(key, value).apply();
    }

    public static int getInt(Context context, String key, int defavalue) {
        SharedPreferences sp = getSp(context);
        return sp.getInt(key, defavalue);
    }

    public static void putLong(Context context, String key, long value) {
        SharedPreferences sp = getSp(context);
        sp.edit().putLong(key, value).apply();
    }

    public static long getLong(Context context, String key, long defavalue) {
        SharedPreferences sp = getSp(context);
        return sp.getLong(key, defavalue);
    }

    /**
     * 保存设备分组List
     * @param tag
     * @param datalist
     */
    public static void setPassengerList(Context context,String tag, List<UpPKGHelp.Passenger> datalist) {
        if (null == datalist || datalist.size() <= 0)
            return;

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        SharedPreferences sp = getSp(context);
        sp.edit().putString(tag, strJson).apply();
    }

    /**
     * 获取设备分组List
     * @param tag
     * @return
     */
    public static List<UpPKGHelp.Passenger> getPassengerList(Context context,String tag) {
        List<UpPKGHelp.Passenger> datalist=new ArrayList<>();

        SharedPreferences sp = getSp(context);

        String strJson = sp.getString(tag, null);
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson, new TypeToken<List<UpPKGHelp.Passenger>>() {}.getType());

        return datalist;
    }

    /**
     * 保存展示数据坐标
     * @param context
     * @param tag
     * @param datalist
     */
    public static void setPassFlowData(Context context,String tag, List<Integer> datalist) {
        if (null == datalist || datalist.size() <= 0)
            return;

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        SharedPreferences sp = getSp(context);
        sp.edit().putString(tag, strJson).apply();
    }

    /**
     * 保存展示数据坐标
     * @param tag
     * @return
     */
    public static List<Integer> getPassFlowData(Context context,String tag) {
        List<Integer> datalist=new ArrayList<>();

        SharedPreferences sp = getSp(context);

        String strJson = sp.getString(tag, null);
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson, new TypeToken<List<Integer>>() {}.getType());

        return datalist;
    }

    /**
     * 保存设备key
     * @param accessKey
     * @param secretKey
     */
    public static void writeAccessKey(Context context,String accessKey, String secretKey) {
        putString(context,"accessKey",accessKey);
        putString(context,"secretKey",secretKey);
    }

    public static String getAccessKey(Context context){
        return getString(context,"accessKey");
    }

    public static String getSecretKey(Context context){
        return getString(context,"secretKey");
    }
}
